package ffhs.lernkartei;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import ffhs.lernkartei.persistence.Stack;
import ffhs.lernkartei.persistence.StacksDataSource;

public class AddStackActivity extends Activity {

	// the datasource
	private StacksDataSource datasource;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_stack);

		datasource = new StacksDataSource(this);
		datasource.open();
	}

	/**
	 * Creates a new stack in the database unless the stack name is empty or a
	 * stack with the same name already exists.
	 * 
	 * Will be called via the onClick attribute of the buttons in ad_stack.xml
	 * 
	 * @param view
	 */
	public void onClick(View view) {
		@SuppressWarnings("unchecked")
		Stack stack = null;
		switch (view.getId()) {
		case R.id.AddStack_button_add:
			EditText stackname = (EditText) findViewById(R.id.AddStack_editText_stackname);
			// Save the new comment to the database
			if (!stackname.getText().toString().equals("")
					&& datasource
							.getStackByName(stackname.getText().toString()) == null) {
				stack = datasource.createStack(stackname.getText().toString());
				Toast.makeText(this, "Stapel wurde erstellt!",
						Toast.LENGTH_LONG).show();
			}
			break;
		}

		Intent i = new Intent(AddStackActivity.this, AllStacksActivity.class);
		startActivity(i);
	}

}